<template>
  <el-dialog
    :visible.sync="dialogOpen"
    :title="dialogTitle"
    :before-close="cancel"
    width="35%"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="服务器名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="服务器IP" prop="ip">
            <el-input v-model="formData.ip" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="端口" prop="port">
            <el-input v-model="formData.port" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="账号" prop="account">
            <el-input v-model="formData.account" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="formData.password"
              type="password"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="warning" @click="handleConnect">测试连接</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<style scoped>
.dialog-footer {
  text-align: center;
}
</style>
<script>
import { addServer, editServer, testConnect } from "@/api/repository";

export default {
  name: "Edit",
  props: [
    "dialogTitle", //标题
    "dialogOpen", //显示
    "dialogData",
  ],
  data() {
    return {
      interfaceDatas: [], //数据接口
      interfaceAddress: "",
      interfacePort: 0,
      formData: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "服务器名称不能为空", trigger: "blur" }],
        ip: [{ required: true, message: "IP不能为空", trigger: "blur" }],
        port: [{ required: true, message: "端口不能为空", trigger: "blur" }],
        account: [{ required: true, message: "账号不能为空", trigger: "blur" }],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
      },
    };
  },
  watch: {},
  created() {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.formData = this.dialogData;
    },
    //测试连接
    handleConnect() {
      testConnect(this.formData).then((res) => {
        console.log("testConnect", res);
        if (res.code == 200) {
          this.$message({ message: "连接成功", type: "success" });
        } else {
          this.$message({ message: res.message, type: "error" });
        }
      });
    },
    handleChangeInterface(val) {
      this.interfaceDatas.forEach((item) => {
        if (item.id == val) {
          this.interfaceAddress = item.interfaceMulticastAddress;
          this.interfacePort = item.interfacePort;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.$emit("closeDialog");
    },
    // 表单重置
    reset() {
      this.formData = {
        sourceName: undefined,
        sourceCode: undefined,
        sourceType: undefined,
        sourceDesc: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        console.log("submitForm", this.formData);
        if (valid) {
          if (this.formData.id != undefined) {
            console.log("update");
            editServer(this.formData).then((response) => {
              console.log(response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          } else {
            addServer(this.formData).then((response) => {
              console.log("add", response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          }
        }
      });
    },
  },
};
</script>
